import java.awt.GraphicsConfiguration;
import java.awt.HeadlessException;

import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
/**
 * create and display GUI that allow user to Generate reports.
 *
 */
public class BookReportGUI extends JFrame {

	private static final long serialVersionUID = 1L;
	/**
	 * Main Panel
	 */
	public JPanel jContentPane = null;
	/**
	 *  Pointer to control
	 */
	public BookReportControl cont;
	/**
	 * Allow to display Book statistic report
	 */
	public BookReportResultPGUI brpg;
	/**
	 * Allow the user to select date range
	 */
	public BookReportDateGUI brdg;
	/**
	 * temporary String
	 */
	public String title;
	/**
	 * temporary String[]
	 */
	public String[] result_temp;
	 
	/**
	 * Create and initializes  BookReportGUI
	 * @param cont1 Pointer to BookReportGUI control
	 * @throws HeadlessException
	 */
	public BookReportGUI(BookReportControl cont1) throws HeadlessException {
		// TODO Auto-generated constructor stub
		super();
		initialize();
		cont=cont1;
	}

	public BookReportGUI(GraphicsConfiguration arg0) {
		super(arg0);
		// TODO Auto-generated constructor stub
		initialize();
	}

	public BookReportGUI(String arg0) throws HeadlessException {
		super(arg0);
		// TODO Auto-generated constructor stub
		initialize();
	}

	public BookReportGUI(String arg0, GraphicsConfiguration arg1) {
		super(arg0, arg1);
		// TODO Auto-generated constructor stub
		initialize();
	}

	/**
	 * initializes BookReportGUI.
	 * Create brpg and Set button jbtnBack to return to SearchBook
	 * Create brdg and Set button jbtnNext1 to Check the user start and end dates,and Generate Book statistic Report.
	 * Set brdg.jbtnBack1 button to return to SearchBook
	 * @return void
	 */
	private void initialize() {
		this.setSize(300, 264);
		this.setContentPane(getJContentPane());
		this.setTitle("Book Report");
		brpg=new BookReportResultPGUI();
			brpg.jbtnBack.addActionListener(new java.awt.event.ActionListener() {
			public void actionPerformed(java.awt.event.ActionEvent e) {
				Main.log.frame.setVisible(true);
				Main.log.frame.setEnabled(true);
				setVisible(false);
				setEnabled(false);
			}
		});
			brdg=new BookReportDateGUI();
			brdg.jbtnNext1.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					brdg.intDate[0]=Integer.parseInt(brdg.jInMon.getSelectedItem().toString());
					brdg.intDate[1]=Integer.parseInt(brdg.jInDay.getSelectedItem().toString());
					brdg.intDate[2]=Integer.parseInt(brdg.jInYear.getSelectedItem().toString());
					brdg.outDate[0]=Integer.parseInt(brdg.jOutMon.getSelectedItem().toString());
					brdg.outDate[1]=Integer.parseInt(brdg.jOutDay.getSelectedItem().toString());
					brdg.outDate[2]=Integer.parseInt(brdg.jOutYear.getSelectedItem().toString());
					if((brdg.intDate[2]>brdg.outDate[2]))
						JOptionPane.showMessageDialog(null,"Date is Wrong","ERROR",JOptionPane.ERROR_MESSAGE);
					else if ((brdg.intDate[2]==brdg.outDate[2])&&(brdg.intDate[0]>brdg.outDate[0]))
						JOptionPane.showMessageDialog(null,"Date is Wrong","ERROR",JOptionPane.ERROR_MESSAGE);
					else if ((brdg.intDate[2]==brdg.outDate[2])&&(brdg.intDate[0]==brdg.outDate[0])&&(brdg.intDate[1]>brdg.outDate[1]))
						JOptionPane.showMessageDialog(null,"Date is Wrong","ERROR",JOptionPane.ERROR_MESSAGE);
					else
						cont.lSearchBooks(result_temp[0], brdg.intDate, brdg.outDate);
				}
			});
			brdg.jbtnBack1.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					Main.log.frame.setVisible(true);
					Main.log.frame.setEnabled(true);
					Main.searchBookG.flag=0;
					setVisible(false);
					setEnabled(false);
				}
			});
		
	}

	/**
	 *initializes jContentPane
	 * 
	 * @return jContentPane JPanel
	 */
	private JPanel getJContentPane() {
		if (jContentPane == null) {
			jContentPane = new JPanel();
			jContentPane.setLayout(null);
		}
		return jContentPane;
	}

	/**
	 * Set data on BookReportResultPGUI and Display it.
	 * @param bookId The Book serial number
	 * @param pop The book popularity
	 */
	public void setTextp(String bookId,float pop)
	{
		brpg.setTextP(bookId, pop);
		setContentPane(brpg);
	}
	/**
	 * Receive Book Serial number set data on BookReportDateGUI and Display it.
	 * @param st The wanted book serial
	 */
	public void dateUpload(String st)
	{
		result_temp=st.split("\\:");
		title="Select Date for BookId:"+result_temp[0];
		brdg.jlblTitle.setText(title);
		Main.log.frame.setVisible(false);
		Main.log.frame.setEnabled(false);
		jContentPane.setVisible(true);
		setEnabled(true);
		setContentPane(brdg);
	}
	

}  
